package com.link2room.rodimus.controller.api;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.rodimus.dto.param.api.*;
import com.link2room.rodimus.facade.IGroupBuyingFacadeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * Created by LN on 2017/12/5
 */
@Api(tags = "团购相关")
@RestController
@RequestMapping("/api/GroupBuying")
public class GroupBuyingController {
    @Resource
    private IGroupBuyingFacadeService groupBuyingFacadeService;
    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto list(
            @RequestParam(name = "groupBuyingCode",required = false)String groupBuyingCode,
            @RequestParam(name = "order",required = false)String order,
            @RequestParam(name = "sortBy",required = false)String sortBy,
            @RequestParam(name = "isHalt",required = false)String isHalt
    ) throws Exception {

        GroupBuyingListParamDto paramDto = new GroupBuyingListParamDto();
        paramDto.setGroupBuyingCode(groupBuyingCode);
        paramDto.setOrder(order);
        paramDto.setSortBy(sortBy);
        paramDto.setIsHalt(isHalt);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(groupBuyingFacadeService.groupBuyingBaseList(paramDto));
        return resDto;
    }
    @RequestMapping(path = "/{groupBuyingCode}",method = RequestMethod.GET)
    public BaseResultDto list(

            @PathVariable("groupBuyingCode") String groupBuyingCode) throws Exception {

        GroupBuyingListParamDto paramDto = new GroupBuyingListParamDto();
        paramDto.setGroupBuyingCode(groupBuyingCode);

        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingFacadeService.groupBuyingBase(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/groupBuyingTeam",method = RequestMethod.POST)
    public BaseResultDto createGroupBuyingTeam(
            @RequestBody GroupBuyingTeamCreateParamDto paramDto
            ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingFacadeService.createGroupBuyingTeam(paramDto));
        return resDto;
    }
    @RequestMapping(path = "/joinTheTeam",method = RequestMethod.POST)
    public BaseResultDto joinTheTeam(
            @RequestBody GroupBuyingJoinTheTeamParamDto paramDto
    ) throws Exception {
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingFacadeService.joinTheTeam(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/groupBuyingTeams",method = RequestMethod.GET)
    public BaseResultDto groupBuyingTeams(
        @RequestParam(name = "teamCode",required = false)String teamCode,
        @RequestParam(name = "groupBuyingCode",required = false)String groupBuyingCode,
        @RequestParam(name = "sta",required = false)String sta,
        @RequestParam(name = "catpatinNickName",required = false)String catpatinNickName,
        @RequestParam(name = "catpatinOpenid",required = false)String catpatinOpenid
    ) throws Exception {
        GroupBuyingTeamFindParamDto paramDto = new GroupBuyingTeamFindParamDto();
        paramDto.setCatpatinNickName(catpatinNickName);
        paramDto.setCatpatinOpenid(catpatinOpenid);
        paramDto.setSta(sta);
        paramDto.setTeamCode(teamCode);
        paramDto.setGroupBuyingCode(groupBuyingCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(groupBuyingFacadeService.groupBuyingTeamList(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/groupBuyingTeams/{teamCode}",method = RequestMethod.GET)
    public BaseResultDto groupBuyingTeam(
            @PathVariable("teamCode") String teamCode) throws Exception {
        GroupBuyingTeamFindParamDto paramDto = new GroupBuyingTeamFindParamDto();
        paramDto.setTeamCode(teamCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingFacadeService.groupBuyingTeam(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/currentUserTeams",method = RequestMethod.GET)
    public BaseResultDto currentUserTeams(
            @RequestParam(name = "groupBuyingCode",required = false)String groupBuyingCode,
            @RequestParam(name = "sta",required = false)String[] stas
    ) throws Exception {
        GroupBuyingCurrentUserTeamFindParamDto paramDto = new GroupBuyingCurrentUserTeamFindParamDto();
        paramDto.setGroupBuyingCode(groupBuyingCode);
        paramDto.setStas(Arrays.asList(stas));
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(groupBuyingFacadeService.currentUserTeams(paramDto));
        return resDto;
    }
    @RequestMapping(path = "/currentUserTeams/{teamCode}",method = RequestMethod.GET)
    public BaseResultDto currentUserTeams(@PathVariable("teamCode") String teamCode) throws Exception {
        GroupBuyingTeamFindParamDto paramDto = new GroupBuyingTeamFindParamDto();
        paramDto.setTeamCode(teamCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingFacadeService.currentUserTeam(paramDto));
        return resDto;
    }


}
